<?php

include '../../db.php';
$connect = pg_connect($connectionString);
if (!$connect) {
    echo "connect : A connection to the server could not be established!";
    return false;
}

$params = array();
array_push($params, pg_escape_string(strtoupper($_POST["name"])));
array_push($params, pg_escape_string(strtoupper($_POST["code"])));
array_push($params, pg_escape_string(strtoupper($_POST["type"])));
//array_push($params, pg_escape_string($_POST["cost"]));
//array_push($params, pg_escape_string($_POST["price"]));
//array_push($params, pg_escape_string($_POST["quantity"]));
//array_push($params, pg_escape_string($_POST["fullname"]));
array_push($params, pg_escape_string($_POST["category_id"]));
array_push($params, pg_escape_string($_POST["subcategory_id"]));
array_push($params, pg_escape_string($_POST["brand_id"]));
array_push($params, pg_escape_string($_POST["model_id"]));
array_push($params, pg_escape_string($_POST["scaffold_id"]));
array_push($params, pg_escape_string($_POST["file_id"]));
array_push($params, pg_escape_string($_POST["position_id"]));
//array_push($params, pg_escape_string($_POST["unit_id"]));

$full_name = "";
$location = "";
$str_category = "";


//----------------------------------------------------------------------
$result = pg_query($connect, "SELECT id, name FROM warehouse_category WHERE id = '" . $_POST["category_id"] . "'");
$arrRegistros = pg_fetch_all($result);
$str_category .= $arrRegistros[0]['name'];
pg_free_result($result);
//----------------------------------------------------------------------
$result = pg_query($connect, "SELECT id, name FROM warehouse_subcategory WHERE id = '" . $_POST["subcategory_id"] . "'");
$arrRegistros = pg_fetch_all($result);
if ($arrRegistros[0]['name'] != "") {
    $str_category .= " - " . $arrRegistros[0]['name'];
}
pg_free_result($result);
//----------------------------------------------------------------------
$result = pg_query($connect, "SELECT id, name FROM warehouse_brand WHERE id = '" . $_POST["brand_id"] . "'");
$arrRegistros = pg_fetch_all($result);
$full_name .= $arrRegistros[0]['name'] . " ";
pg_free_result($result);
//----------------------------------------------------------------------
$result = pg_query($connect, "SELECT id, name FROM warehouse_model WHERE id = '" . $_POST["model_id"] . "'");
$arrRegistros = pg_fetch_all($result);
$full_name .= $arrRegistros[0]['name'] . " ";
pg_free_result($result);
//----------------------------------------------------------------------
$result = pg_query($connect, "SELECT id, name FROM warehouse_scaffold WHERE id = '" . $_POST["scaffold_id"] . "'");
$arrRegistros = pg_fetch_all($result);
$location .= "Andamio :" . $arrRegistros[0]['name'] . " ";
pg_free_result($result);
//----------------------------------------------------------------------
$result = pg_query($connect, "SELECT id, name FROM warehouse_file WHERE id = '" . $_POST["file_id"] . "'");
$arrRegistros = pg_fetch_all($result);
$location .= "Fila :" . $arrRegistros[0]['name'] . " ";
pg_free_result($result);
//----------------------------------------------------------------------
$result = pg_query($connect, "SELECT id, name FROM warehouse_position WHERE id = '" . $_POST["position_id"] . "'");
$arrRegistros = pg_fetch_all($result);
$location .= "Posicion :" . $arrRegistros[0]['name'] . " ";
pg_free_result($result);
//----------------------------------------------------------------------

$full_name .= $_POST["type"] . " " . $_POST["name"];
//------------------------------------------------------------------------------
array_push($params, pg_escape_string(strtoupper($full_name)));
array_push($params, pg_escape_string(strtoupper($location)));
array_push($params, pg_escape_string(strtoupper($str_category)));
//------------------------------------------------------------------------------


$stmt = pg_prepare($connect, "create_product", "INSERT INTO warehouse_product("
        . "name,code,type,"
        //. "cost,price,"
        //. "quantity,"
        //. "fullname,"
        . "category_id,subcategory_id,brand_id,model_id,scaffold_id,file_id,position_id,fullname, location, str_category"
        . //",unit_id
        ") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13"
        //. ",$15"
        . ");");
$result = pg_execute($connect, "create_product", $params);

if (!$result) {
    die("Error in SQL query: " . pg_last_error());
}
pg_free_result($result);
//



pg_close($connect);


